<?php
require_once 'admin_inc_path.php';

//data submission for left column

if (isset($_POST['slc'])):
    $lc = DAOFactory::getPA10DAO()->queryBySeg($_POST['seg']);
    if (empty($lc)):
        $sh = new SubHeading();
        $des = new Description();
    else:
        $sh = DAOFactory::getSubHeadingDAO()->load($lc[0]->que);
        $des = DAOFactory::getDescriptionDAO()->load($lc[0]->ans);
    endif;

    $sh->textEn = $_POST['sub_heading_en'];
    $sh->textSv = $_POST['sub_heading_sv'];
    $des->textEn = $_POST['description_en'];
    $des->textSv = $_POST['description_sv'];

    if (empty($lc)):
        $sh_id = DAOFactory::getSubHeadingDAO()->insert($sh);
        $des_id = DAOFactory::getDescriptionDAO()->insert($des);
        $pA10 = new PA10();
        $pA10->que = $sh_id;
        $pA10->ans = $des_id;
        $pA10->seg = $_POST['seg'];
        $response = DAOFactory::getPA10DAO()->insert($pA10);
        //echo 'New left segment inserted:- ' . var_dump($response);
        if($response):
            echo __INSERTED__;
        endif;
    else:
        $sh_id = DAOFactory::getSubHeadingDAO()->update($sh);
        $des_id = DAOFactory::getDescriptionDAO()->update($des);
        //echo "Left segment have updated:- " . var_dump($sh_id, $des_id);
        if($sh_id||$des_id):
            echo __UPDATED__;
            
        endif;
    endif;

endif;

if(isset($_POST['cfsub'])):
    $cforums = DAOFactory::getCONTACTDAO()->queryAll();
        if($cforums):
            $cforum = $cforums[0];
            else:
                $cforum = new CONTACT();
        endif;
        
        $cforum->hEADINGEN = $_POST['heading_en'];
        $cforum->hEADINGSV = $_POST['heading_sv'];
        $cforum->nAMEEN = $_POST['name_en'];
        $cforum->nAMESV = $_POST['name_sv'];
        $cforum->eMAILEN = $_POST['email_en'];
        $cforum->eMAILSV = $_POST['email_sv'];
        $cforum->tEXTEN = $_POST['message_en'];
        $cforum->tEXTSV = $_POST['message_sv'];
        $cforum->bUTTONEN = $_POST['button_en'];
        $cforum->bUTTONSV = $_POST['button_sv'];
        
        if(!empty($cforums)):
            $res= DAOFactory::getCONTACTDAO()->update($cforum);
                if($res):
                    echo __UPDATED__;
                endif;
        else:
            $res = DAOFactory::getCONTACTDAO()->insert($cforum);
             if($res):
                  echo __INSERTED__;
             endif;
        endif;
        
        
endif;

//new data insertion for page contents
if (isset($_POST['snm'])) :

    $sh = new SubHeading();
    $des = new Description();
    $imgF = new Image();
    $imgS = new Image();

    $sh->textEn = $_POST['f_en'];
    $sh->textSv = $_POST['f_sv'];

    $des->textEn = $_POST['s_en'];
    $des->textSv = $_POST['s_sv'];

    
    $imgContF = new FileController();
    if ($imgContF->attach_file($_FILES['image_F']) && $imgContF->process_image(_IMAGE_, 'index_page')) {
        $imgF->name = $imgContF->optimized_name;
        $imgF->path = 'temp';
    } else {
        echo __PIMGUP__;
        //print_r($imgContF->errors);
    }
    
    $imgContS = new FileController();
    if ($imgContS->attach_file($_FILES['image_S']) && $imgContS->process_image(_IMAGE_, 'index_page')) {
        $imgS->name = $imgContS->optimized_name;
        $imgS->path = 'temp';
    } else {
        echo __PIMGUP__;
        //print_r($imgContS->errors);
    }

    $pA10 = new PA10();
    $pA10->seg = $_POST['seg'];
    $pA10->que = DAOFactory::getSubHeadingDAO()->insert($sh);
    $pA10->ans = DAOFactory::getDescriptionDAO()->insert($des);
    $pA10->imgQ = DAOFactory::getImageDAO()->insert($imgF);
    $pA10->imgA = DAOFactory::getImageDAO()->insert($imgS);

    $response = DAOFactory::getPA10DAO()->insert($pA10);
    if($response):
        echo __INSERTED__;
    endif;
    //print_r($response);

endif;


//updation for page contents
if (isset($_POST['update'])):

    $pA10 = DAOFactory::getPA10DAO()->load($_POST['ref_pc']);
    //print_r($pA10); die;

    $sh = DAOFactory::getSubHeadingDAO()->load($pA10->que);
    $des = DAOFactory::getDescriptionDAO()->load($pA10->ans);
    $imgF = DAOFactory::getImageDAO()->load($pA10->imgQ);
    $imgS = DAOFactory::getImageDAO()->load($pA10->imgA);

    $sh->textEn = $_POST['f_en'];
    $sh->textSv = $_POST['f_sv'];

    $des->textEn = $_POST['s_en'];
    $des->textSv = $_POST['s_sv'];

    if (isset($_FILES['image_F'])):
        $errors = array();
        $imgCont = new FileController();
        if ($imgCont->attach_file($_FILES['image_F']) && $imgCont->process_image(_IMAGE_, 'index_page')) {
            $imgF->name = $imgCont->optimized_name;
            $imgF->path = 'temp';
        } else {
            //print_r($imgCont->errors);
            echo __PIMGUP__;
        }
    endif;
    
    if (isset($_FILES['image_S'])):
        $errors = array();
        $imgCont = new FileController();
        if ($imgCont->attach_file($_FILES['image_S']) && $imgCont->process_image(_IMAGE_, 'index_page')) {
            $imgS->name = $imgCont->optimized_name;
            $imgS->path = 'temp';
        } else {
            //print_r($imgCont->errors);
            echo __PIMGUP__;
        }
    endif;
    


    $res_sh = DAOFactory::getSubHeadingDAO()->update($sh);
    $res_des = DAOFactory::getDescriptionDAO()->update($des);
    $res_imgF = DAOFactory::getImageDAO()->update($imgF);
    $res_imgS = DAOFactory::getImageDAO()->update($imgS);
    
    if($res_sh||$res_des||$res_imgF):
        echo __UPDATED__;
    endif;
    //echo'value update:-- ' . var_dump($res_sh, $res_des, $res_imgF,$res_imgS);

endif;

//for deletion
if (isset($_POST['del'])):
    $response = DAOFactory::getPA10DAO()->delete($_POST['ref_pc']);
    if($response):
        echo __REMOVED__;
    endif;
    //echo 'Info deleted:--' . $response;
endif;




include_once('inc/admin_header.php')
?>		

<div class="admin_content_wrapper">


    <h1>Forum</h1>
    <h2>Left Column</h2>
<?php
//fetching data for left column
$mm = DAOFactory::getMainMenuDAO()->load(5);
$head = DAOFactory::getMainHeadingDAO()->load($mm->mH);
$lc_list = DAOFactory::getPA10DAO()->queryBySeg(0);
$sh = !empty($lc_list) ? DAOFactory::getSubHeadingDAO()->load($lc_list[0]->que) : '';
$des = !empty($lc_list) ? DAOFactory::getDescriptionDAO()->load($lc_list[0]->ans) : '';

$cforums = DAOFactory::getCONTACTDAO()->queryAll();
$cforum = !empty($cforums) ? $cforums[0]: new CONTACT();

//fetching data for page contents
?> 

    <div class="admin_segment">
    <form action="" method="post" >
        
            <table>
                <tr>
                    <td>

                    </td>
                    <td>
                        <h3>Svenska</h3>
                    </td>
                    <td>
                        <h3>Engelska</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Heading:</h3>
                    </td>
                    <td>
                        <p><?php echo $value = (!empty($head->textSv)) ? $head->textSv : __EMT; ?> (Editable by Top Meny)</p>
                    </td>
                    <td>
                        <p><?php echo $value = (!empty($head->textEn)) ? $head->textEn : __EMT; ?> (Editable by Top Meny)</p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Sub Heading:</h3>
                    </td>
                    <td>
                        <input type="text" name="sub_heading_sv" value="<?php echo $value = (!empty($sh->textSv)) ? $sh->textSv : __EMT; ?>" />
                    </td>
                    <td>
                        <input type="text" name="sub_heading_en" value="<?php echo $value = (!empty($sh->textEn)) ? $sh->textEn : __EMT; ?>" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Description:</h3>
                    </td>
                    <td>
                        <textarea name="description_sv"><?php echo $value = (!empty($des->textSv)) ? $des->textSv : __EMT; ?> </textarea>
                    </td>
                    <td>
                        <textarea name="description_en"><?php echo $value = (!empty($des->textEn)) ? $des->textEn : __EMT; ?></textarea>
                    </td>
                </tr>
            </table>
            <input type="hidden" name="seg" value="0" />
            <input type="submit" value="Save and Upload" name="slc"/>
        
    </form>
    
    <h2>Forum</h2>
    
    <form action="" method="POST">
         <table>
                <tr>
                    <td>

                    </td>
                    <td>
                        <h3>Svenska</h3>
                    </td>
                    <td>
                        <h3>Engelska</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Heading:</h3>
                    </td>
                    <td>
                        <input type="text" name="heading_sv" value="<?php echo $cforum->hEADINGSV; ?>"/>
                    </td>
                    <td>
                        <input type="text" name="heading_en" value="<?php echo $cforum->hEADINGEN; ?>"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Name:</h3>
                    </td>
                    <td>
                        <input type="text" name="name_sv" value="<?php echo $cforum->nAMESV; ?>"/>
                    </td>
                    <td>
                        <input type="text" name="name_en" value="<?php echo $cforum->nAMEEN; ?>"/>
                    </td>
                    
                </tr>
                <tr>
                    <td>
                        <h3>Email</h3>
                    </td>
                    <td>
                        <input type="text" name="email_sv" value="<?php echo $cforum->eMAILSV; ?>"/>
                    </td>
                    <td>
                        <input type="text" name="email_en" value="<?php echo $cforum->eMAILEN; ?>"/>
                    </td>
                    
                </tr>
                
                <tr>
                    <td>
                        <h3>Message:</h3>
                    </td>
                    <td>
                        <input type="text" name="message_sv" value="<?php echo $cforum->tEXTSV; ?>"/>
                    </td>
                    <td>
                        <input type="text" name="message_en" value="<?php echo $cforum->tEXTEN; ?>"/>
                    </td>
                    
                </tr>
                <tr>
                    <td>
                        <h3>Send Button:</h3>
                    </td>
                    <td>
                        <input type="text" name="button_sv" value="<?php echo $cforum->bUTTONSV; ?>"/>
                    </td>
                    <td>
                        <input type="text" name="button_en" value="<?php echo $cforum->bUTTONEN; ?>"/>
                    </td>
                    
                </tr>
                
                
         </table>
            <input type="submit" value="Save and Upload" name="cfsub"/>
    </form>
    </div>
    

    <h2>Page Content</h2>

<?php 
     $pc = DAOFactory::getPA10DAO()->queryBySeg(1);
     if(!empty($pc)):
         //print_r($pc);
         foreach ($pc as $key => $value):
            echo $value->id;
            $imgF = DAOFactory::getImageDAO()->load($value->imgQ);
            $imgS = DAOFactory::getImageDAO()->load($value->imgA);
            $que = DAOFactory::getSubHeadingDAO()->load($value->que);
            $ans = DAOFactory::getDescriptionDAO()->load($value->ans);
    ?>


    <form action="" method="POST" enctype="multipart/form-data">
        <div class="admin_segment">
            <h2><!--img src="images/fraga_en.png" /-->Question</h2>
            <table>
                <tr>
                    <td>

                    </td>
                    <td>
                        <h3>Svenska</h3>
                    </td>
                    <td>
                        <h3>Engelska</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Description:</h3>
                    </td>
                    <td>
                        <textarea name="f_sv"><?php echo $que->textSv; ?></textarea>
                    </td>
                    <td>
                        <textarea name="f_en"><?php echo $que->textEn; ?></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Image:</h3>
                    </td>
                    <td>
                        <input type="file" name="image_F"/>
                    </td>
                    <td>
                        <p> <?php echo $imgF->name; ?> </p>    
                    </td>
                </tr>
            </table>

            <h2><!--img src="images/svar_en.png" /-->Answer</h2>
            <table>
                <tr>
                    <td>

                    </td>
                    <td>
                        <h3>Svenska</h3>
                    </td>
                    <td>
                        <h3>Engelska</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Description:</h3>
                    </td>
                    <td>
                        <textarea name="s_sv"><?php echo $ans->textSv ?></textarea>
                    </td>
                    <td>
                        <textarea name="s_en"><?php echo $ans->textEn ?></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Image:</h3>
                    </td>
                    <td>
                        <input type="file" name="image_S"/>
                    </td>
                    <td>
                       <p> <?php echo $imgS->name; ?> </p>        
                    </td>
                </tr>
            </table>
            <input type="hidden" value="1" name="seg"/>
            <input type="submit" value="Update" name="update"/>
            <input type="hidden" value="<?php echo $value->id; ?>" name="ref_pc" />
            <input class="delete_btn" type="submit" value="Delete" name="del"/>
        </div>
    </form>
    
    <?php 
        endforeach;
        endif;
    ?>






    <form action="" method="POST" enctype="multipart/form-data">
        <div class="admin_segment">
            <h2><!--img src="images/fraga_en.png" /-->Question</h2>
            <table>
                <tr>
                    <td>

                    </td>
                    <td>
                        <h3>Svenska</h3>
                    </td>
                    <td>
                        <h3>Engelska</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Description:</h3>
                    </td>
                    <td>
                        <textarea name="f_sv"></textarea>
                    </td>
                    <td>
                        <textarea name="f_en"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Image:</h3>
                    </td>
                    <td>
                        <input type="file" name="image_F"/>
                    </td>
                    <td>

                    </td>
                </tr>
            </table>

            <h2><!--img src="images/svar_en.png" /-->Answer</h2>
            <table>
                <tr>
                    <td>

                    </td>
                    <td>
                        <h3>Svenska</h3>
                    </td>
                    <td>
                        <h3>Engelska</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Description:</h3>
                    </td>
                    <td>
                        <textarea name="s_sv"></textarea>
                    </td>
                    <td>
                        <textarea name="s_en"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Image:</h3>
                    </td>
                    <td>
                        <input type="file" name="image_S"/>
                    </td>
                    <td>

                    </td>
                </tr>
            </table>
             <input type="hidden" value="1" name="seg"/>
            <input type="submit" value="Save and Upload" name="snm" />
           
        </div>
    </form>


</div>
<?php include_once('inc/admin_footer.php') ?>